﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DcsGauges.Gui.Rendering;
using DcsGauges.Gui.Gauges.Interfaces;
using DcsGauges.DataStructures;
using Microsoft.Xna.Framework.Graphics;
using DcsGauges.Input.Processors;
using Microsoft.Xna.Framework;
using DcsGauges.Shared.DataStructures;
using DcsGauges.Input.Processors.Swipe;

namespace DcsGauges.Gui.Gauges.Trigger
{
    class AnalogRotatorSwitchGauge : GuiRenderableBase, ITriggerGauge
    {
        #region Fields & Properties

        private Texture2D                               knobTexture                         = null;
        private IntermediateSwipeProcessor              inputProcessor                      = null;
        private IGaugeValueConverter<float>             valueConverter                      = null;

        #endregion


        #region Consructors

        public AnalogRotatorSwitchGauge(GuiRenderableBase parent, URect area, DcsProperty property, Texture2D knobTexture, 
            IGaugeValueConverter<float> valueConverter) : base(parent, area)
        {
            this.DataProperty                   = property;
            this.knobTexture                    = knobTexture;
            this.valueConverter                 = valueConverter;
            this.inputProcessor                 = new IntermediateSwipeProcessor(this);
            this.inputProcessor.SwipeEvent      += new EventHandler<IntermediateSwipeProcessor.SwipeEventArgs>(HandleInputProcessorSwipeEvent);
        }

        #endregion


        #region Interface Implemenation

        #region Events

        public event EventHandler<TriggerValueEventArgs>  TriggerEvent;

        #endregion

        #region Properties

        public Shared.DataStructures.DcsProperty DataProperty
        {
            get; private set; 
        }

        private float val = 0.0f;
        private float kill;
        public float Value
        {
            get { return this.val; } 
            set { this.kill = 0.0f; }
        }

        #endregion

        #endregion


        #region Overides

        
        public override void Render(SpriteBatch spriteBatch, GraphicsDevice device, Rectangle parentRect, float globalZIndex)
        {
            Rectangle? originalRect                 = base.keepAspectRatio ? this.knobTexture.Bounds : new Rectangle?();
            Rectangle viewportRect                  = base.CalcViewportRect(parentRect, originalRect);
            Vector2 scale                           = base.CalcScaleToFitRect(viewportRect, this.knobTexture.Bounds);

            Point viewportCenter                    = viewportRect.Center;
            Vector2 textureCenter                   = new Vector2((float)this.knobTexture.Bounds.Width / 2.0f, (float)this.knobTexture.Bounds.Height / 2.0f);// new Vector2((float)this.knobTexture.Bounds.Width * scale.X * 0.5f, (float)this.knobTexture.Bounds.Height * scale.Y * 0.5f);

            float rotation                          = this.valueConverter.Convert(this.Value);

            spriteBatch.Draw(this.knobTexture, new Vector2(viewportCenter.X, viewportCenter.Y), null, Color.White, rotation, textureCenter, scale, SpriteEffects.None, base.NextFrontIndex(globalZIndex, 1));
        }

        public override void InjectMouseEvent(DcsGauges.Input.Events.MouseEvent evt, GameTime gt)
        {
            this.inputProcessor.InjectMouseEvent(evt, gt);
        }

        #endregion


        #region Event Handlers

        private void HandleInputProcessorSwipeEvent(object sender, IntermediateSwipeProcessor.SwipeEventArgs e)
        {
            if (e.Axis == IntermediateSwipeProcessor.SwipeAxis.Horizontal)
            {
                float change                = (float)e.Delta * 0.3f * (float)e.GameTime.ElapsedGameTime.TotalSeconds;
                this.val += change;

                if          (this.val < this.valueConverter.MinIn) this.val = this.valueConverter.MinIn;
                else if     (this.val > this.valueConverter.MaxIn) this.val = this.valueConverter.MaxIn;
            }
        }

        #endregion

    }
}
